package com.problem.leetcode;

/**
 * 解题思路:esult有三个值 -1:初始值，0:相等，1:大于，2:小于
 * 判断是否整体有序
 *
 * @author didi
 * @DESCRIPTION
 * @create 2019-04-24 15:53
 **/

public class Problem896 {
    public boolean isMonotonic(int[] A) {
        //result有三个值 -1:初始值，0:相等，1:大于，2:小于
        int result = -1;
        for (int index = 0; index < A.length - 1; index++) {
            if (result > 0) {
                if ((A[index] > A[index + 1] && (result == 1)) || (A[index] < A[index + 1] && (result == 2)) ||
                    (A[index] == A[index + 1] && (result == 1 || result == 2))) {

                } else if (result == 0) {
                    result = A[index] > A[index + 1] ? 1 : ((A[index] < A[index + 1]) ? 2 : 0);
                } else {
                    return false;
                }
            } else {
                result = A[index] > A[index + 1] ? 1 : ((A[index] < A[index + 1]) ? 2 : 0);
            }
        }
        return true;
    }
}
